﻿//
//  Copyright (c) 2008, WebAula S/A 
//  All rights reserved.
//
//  Authors : 
//
//           * Ivan Paulovich (ivan@100loop.com)
//           Blog: http://www.100loop.com/          
//           Messenger: ivanpaulovich@hotmail.com 
//           Twitter: @ivanpaulovich
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MvcThemes
{
    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Nome do roteamento
                "{controller}/{action}/{id}", // URL com parâmetros
                new { controller = "Post", action = "Index", id = UrlParameter.Optional } // Parâmetros padrão
            );
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }



        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            RazorViewEngine engine = (RazorViewEngine)System.Web.Mvc.ViewEngines.Engines[1];

            HttpCookie themeCookie = Request.Cookies["Theme"];

            string themeName = "Default";

            if (themeCookie != null)
                themeName = themeCookie.Value;

            engine.MasterLocationFormats = new string[] {
                    "~/Themes/" + themeName + "/Views/{1}/{0}.cshtml",
                    "~/Themes/" + themeName + "/Views/Shared/{0}.cshtml",
                };

            engine.ViewLocationFormats = new string[] {
                    "~/Themes/" + themeName + "/Views/{1}/{0}.cshtml",
                    "~/Themes/" + themeName + "/Views/Shared/{0}.cshtml",
                };

            engine.PartialViewLocationFormats = new string[] {
                    "~/Themes/" + themeName + "/Views/{1}/{0}.cshtml",
                    "~/Themes/" + themeName + "/Views/Shared/{0}.cshtml",
                };
        }


    }
}